package com.lujianfei.recyclerview.recyclerview_vertical;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.lujianfei.recyclerview.R;
import com.lujianfei.recyclerview.adapter.RecyclerAdapter;
import com.lujianfei.recyclerview.entity.ListItem;
import com.lujianfei.recyclerview.widget.CustomLinearLayout;
import com.lujianfei.recyclerview.widget.CustomRecyclerView;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

public class LongClickActivity extends AppCompatActivity {

    CustomRecyclerView recyclerview;
    CustomLinearLayout recyclerviewContainer;
    Button bt_top;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view_long_click);
        bt_top = findViewById(R.id.bt_top);
        recyclerview = findViewById(R.id.recyclerview);
        recyclerviewContainer = (CustomLinearLayout) recyclerview.getParent();
        recyclerview.setOnItemLongClickListener(new CustomRecyclerView.OnItemLongClickListener() {
            @Override
            public void onItemLongClick(View view) {
                recyclerview.setDisableMove(true);
                toggle();
            }
        });
        recyclerviewContainer.setOnActionUpListener(new CustomLinearLayout.OnActionUpListener() {
            @Override
            public void onActionUp() {
                recyclerview.setDisableMove(false);
            }
        });
        /** 设置列表布局方式 */
        LinearLayoutManager layoutManager= new LinearLayoutManager(this);
        recyclerview.setLayoutManager(layoutManager);
        List<ListItem> listItems = genData();
        RecyclerAdapter recyclerAdapter = new RecyclerAdapter(listItems);
        recyclerview.setAdapter(recyclerAdapter);
    }

    private void toggle() {
        bt_top.setVisibility(bt_top.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
    }

    private List<ListItem> genData() {
        List<ListItem> listItems = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            listItems.add(new ListItem("Title "+i,"Summary "+i));
        }
        return listItems;
    }

}
